package com.neusoft.jxc.controller;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.jxc.bean.Result;
import com.neusoft.jxc.bean.SysUser;
import com.neusoft.jxc.service.SysUserService;

/**
 * 
 * @author chens
 *
 */
@Controller
public class LoginController {
	Logger logger = Logger.getLogger(LoginController.class);

	@Autowired
	private SysUserService userService;

	@RequestMapping("login")
	@ResponseBody
	public Result login(SysUser user, String validate, HttpSession session) {

		Result result = new Result(Boolean.FALSE, "登录失败");
		String rand = (String) session.getAttribute("rand");
		if (rand != null && rand.equalsIgnoreCase(validate)) {
			SysUser loginSysUser = userService.login(user);
			if (loginSysUser != null) {
				logger.info("登录成功： " + loginSysUser);
				session.setAttribute("loginUser", loginSysUser);
				result = new Result(Boolean.TRUE, "登录成功");
			}
		} else {
			result = new Result(Boolean.FALSE, "验证码错误");
		}

		return result;
	}
	
	@RequestMapping("logout")
	public String logout(HttpSession session) {
		//使用登录信息失效，会话结束
		session.invalidate();
		//重定向：
		return "redirect:/login.jsp";
	}
}
